import 'dart:collection';

import 'package:dio/dio.dart';
import 'package:tender_flutter/constant/api_path.dart';
import 'package:tender_flutter/constant/app_config.dart';
import 'package:tender_flutter/http/api_response.dart';
import 'package:tender_flutter/http/http_utils.dart';

class ApiSubscription {
  /// 订阅列表
  static Future subscriptionList<T>(
      {int page = AppConfig.page, int pageCount = AppConfig.pageCount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["page"] = page;
      params["size"] = pageCount;
      final response =
          await HttpUtils.get(ApiPath.subscriptionList, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 订阅列表的最新数量
  static Future subscriptionBadgeNum<T>() async {
    try {
      final response = await HttpUtils.get(ApiPath.subscriptionBadgeNum);
      return ApiResponse<T>.completed(response, ignoreTip: true);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 订阅类别
  static Future subscriptCategory<T>() async {
    try {
      final response = await HttpUtils.get(ApiPath.subscriptionCategory);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 添加订阅
  static Future addSubscript<T>({
    int id,
    List<int> areaCodes,
    bool isPush,
    String type,
    List<String> keywords,
    // List<String> types,
    // List<String> biddingClasses,
    // List<String> industries,
    String time,
  }) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["id"] = id;
      params["area_ids"] = areaCodes;
      params["isPush"] = isPush ? 1 : 0;
      params["type"] = type;
      // params["biddingClasses"] = biddingClasses;
      // params["industries"] = industries;
      // params["types"] = types;
      params["keyword"] = keywords;
      params["begin"] = time;
      final response =
          await HttpUtils.postRaw(ApiPath.addSubscription, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 删除订阅
  static Future deleteSubscription<T>({int subId}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["id"] = subId;
      final response =
          await HttpUtils.get(ApiPath.deleteSubscription, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 导出订阅
  static Future exportSubscription<T>({String email}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["email"] = email;
      final response =
          await HttpUtils.get(ApiPath.exportSubscription, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 订阅标讯列表
  /// id 订阅分组id
  static Future subscriptionTenderList<T>(
      {int id,
      int page = AppConfig.page,
      int pageCount = AppConfig.pageCount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["id"] = id;
      params["page"] = page;
      params["size"] = pageCount;
      final response =
          await HttpUtils.get(ApiPath.subscriptionTenderList, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 获取热门搜索关键词
  static Future getSubscriptionHotKeywords<T>() async {
    try {
      final response = await HttpUtils.get(ApiPath.subscriptionHotKeywords);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }
}
